<template>
    <div class="userlist-pasenge" :class="[{'empty':showEmpty}]">
        <template v-if="!isLoadDb">
            <page-loading></page-loading>
        </template>
        <template v-else>
            <div v-if="showEmpty" class="div-empty">
                <span class="iconfont icon-meiyoukehu"></span>
                <p class="empty-tip">您还没有客户~</p> 
            </div>
            <ul v-else class="cell-wapper" v-infinite-scroll="requestData" infinite-scroll-disabled="cellLoading" infinite-scroll-distance="20" infinite-scroll-immediate-check="false">
                <li v-for="client in clientsdata">
                    <custom-cell class="custom-cell" :headimgurl="client.headimgurl" :conndate="client.conndate" :phone="client.phone||'暂无'" :username="client.username"></custom-cell>
                </li>
            </ul>
        </template>
    </div>
</template>

<script>
import pageLoading from "../loading/pageLoading.vue";
import Vue from "vue";
import { mapState } from "vuex";
import customCell from "./customCell.vue";
import { InfiniteScroll } from "mint-ui";
Vue.use(InfiniteScroll);

export default {
  name: "user-list",
  data() {
    return {
      isLoadDb: false,
      showEmpty: false,
      clientsdata: [],
      cellLoading: true,
      curPage: 0,
      pageNum: 10,
      existIdArr: [] //保存已经显示的订单id
    };
  },
  components: {
    customCell,
    pageLoading
  },
  mounted() {
    this.showEmpty = false;
    this.requestData(false);
  },
  computed: {
    ...mapState(["userid", "path"])
  },
  methods: {
    requestData(infinite = true) {
      if (infinite && this.curPage == 0) return;
      if (infinite && this.cellLoading) return;
      this.cellLoading = true;
      this.$http
        .post(this.path + `store/User/userlist`, {
          uid: this.userid,
          page: this.curPage
        })
        .then(response => {
          this.setLoadDb();
          let serverData = response.body.result;
          if (!serverData || serverData.length == 0) {
            //数据为空
            this.cellLoading = true;
            if (this.curPage == 0) this.showEmpty = true;
          } else {
            //有数据
            if (serverData.length % 10 == 0) {
              //满一页数据
              this.curPage++;
              this.cellLoading = false;
            } else {
              //不满一页
              this.cellLoading = true;
            }
            for (let i = 0; i < serverData.length; i++) {
              let listid = serverData[i].listid;
              if (this.existIdArr.indexOf(listid) >= 0) continue;
              this.clientsdata.push(serverData[i]);
              this.existIdArr.push(listid);
            }
          }
        });
    },
    setLoadDb() {
      if (this.isLoadDb) return;
      let clearId = setTimeout(() => {
        clearTimeout(clearId);
        this.isLoadDb = true;
      }, 200);
    }
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.userlist-pasenge {
  width: 100%;
  min-height: 100%;
  background-color: #efefef;

  &.empty {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    background-color: #fff;
  }

  .div-empty {
    margin-bottom: 25vh;

    .icon-meiyoukehu {
      font-size: 1.2rem;
      color: #595656;
    }

    .empty-tip {
      margin-top: 0.26rem;
      font-size: 0.13rem;
      text-align: center;
      color: #595656;
      letter-spacing: 0.01rem;
    }
  }

  .cell-wapper {
    width: 100%;

    li {
      width: 100%;
      background-color: #fff;
      margin-bottom: 0.06rem;

      .custom-cell {
        width: 100%;
      }
    }
  }
}
</style>
